package com.LeeCode;

/**
 * 交替合并字符串
 */

public class Code1768 {
    public static void main(String[] args) {
        String word1 = "abc";
        String word2 = "pqrs";
        System.out.println(new Code1768().mergeAlternately(word1, word2));
    }

    public String mergeAlternately(String word1, String word2) {
        StringBuilder sb = new StringBuilder();

        int len1 = word1.length(), len2 = word2.length();
        int index1 = 0, index2 = 0;

        while (index1 < len1 && index2 < len2) {
            sb.append(word1.charAt(index1));
            index1++;
            sb.append(word2.charAt(index2));
            index2++;
        }
        if (index1 < len1) {
            sb.append(word1.substring(index1));
        }
        if (index2 < len2) {
            sb.append(word2.substring(index2));
        }

        return sb.toString();
    }
}
